package tk.hoopy.zedtown.assets;

import java.io.File;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class AssetManager {
	
	public static final String RESOURCES = "res//";
	public static final String DATA = RESOURCES + "data//";
	public static final String TEXTURES = RESOURCES + "textures//";
	public static final String OBJECTS = DATA + "objects//";
	public static final Tile NULL_TILE = new Tile();
	
	private static ArrayList<Tile> tiles = new ArrayList<Tile>();
	private static ArrayList<Entity> entities = new ArrayList<Entity>();
	
	public static void LoadAssets() {
		NULL_TILE.SetTexture(new ImageIcon(TEXTURES+"//tile//error.png").getImage());
		tiles.add(NULL_TILE);
		File objects = new File(OBJECTS);
		LoadFromDirectory(objects);
	}
	
	private static void LoadFromDirectory(File d) {
		XMLReader reader = new XMLReader();
		String[] objectList = d.list();
		for(int i=0;i<objectList.length;i++) {
			if(new File(d.getPath()+"//"+objectList[i]).isDirectory()) {
				LoadFromDirectory(new File(d.getPath()+"//"+objectList[i]));
			} else {
				Asset newAsset = reader.ParseDocument(new File(d.getPath()+"//"+objectList[i]));
				if(newAsset.IsTile()) {
					tiles.add((Tile) newAsset);
					System.out.println("Added new Tile: "+newAsset.GetName());
				} else if(newAsset.IsEntity()) {
					entities.add((Entity) newAsset);
					System.out.println("Added new Entity: "+newAsset.GetName());
				}
			}
		}
	}
	
	public static Tile GetTileFromName(String name) {
		for(int i=0;i<tiles.size();i++) {
			if(name.equals(tiles.get(i).GetName())) {
				return tiles.get(i);
			}
		}
		return NULL_TILE;
	}
	
}
